package flc.ast.activity;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.CityJsonBean;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityJobIntentionBinding;
import flc.ast.dialog.ArrivalTimeDialog;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.CityLevelsListDate;
import flc.ast.utils.ResumeDataUtils;

public class JobIntentionActivity extends BaseAc<ActivityJobIntentionBinding> {

    public static int position;
    public static int modulePos;
    private ResumeBean resumeBean;
    private ResumeBean.JobData jobData;
    private ResumeBean.Module module;
    private OptionsPickerView cityOptions;
    private CityLevelsListDate levelsListDate;
    private ArrayList<CityJsonBean> jsonBeans;
    private ArrayList<ArrayList<String>> arrayLists;

    @Override
    protected int onCreate() {
        return R.layout.activity_job_intention;
    }

    @Override
    protected void initView() {
        mDataBinding.ivJobIntentionBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);
        mDataBinding.llIntendedCity.setOnClickListener(this);
        mDataBinding.llArrivalTime.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                save();
                break;
            case R.id.tvTitle://标题
                showRenameDialog();
                break;
            case R.id.llIntendedCity://意向城市
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    KeyboardUtils.hideSoftInput(this);
                }
                if (cityOptions != null) {
                    cityOptions.show();
                }
                break;
            case R.id.llArrivalTime://到岗时间
                showArrivalTimeDialog();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        if (module.getData() != null) {
            jobData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.JobData.class);
            mDataBinding.etExpectedPosition.setText(jobData.getTargetJob());
            mDataBinding.tvIntendedCity.setText(jobData.getTargetCity());
            mDataBinding.etExpectedSalary.setText(jobData.getTargetSalary());
            mDataBinding.tvArrivalTime.setText(jobData.getEntryTime());
        } else {
            jobData = new ResumeBean.JobData();
        }
        levelsListDate = new CityLevelsListDate(mContext);
        jsonBeans = levelsListDate.initJsonData("citys_data.json");
        arrayLists = levelsListDate.initJsonData1("citys_data.json");
        showCityView();
    }

    private void save() {
        String expectedPosition = mDataBinding.etExpectedPosition.getText().toString().trim();
        if (TextUtils.isEmpty(expectedPosition)) {
            ToastUtils.showShort(getString(R.string.please_input_expected_position));
            return;
        }
        String intendedCity = mDataBinding.tvIntendedCity.getText().toString().trim();
        if (TextUtils.isEmpty(intendedCity)) {
            ToastUtils.showShort(getString(R.string.please_sel_intended_city));
            return;
        }
        String expectedSalary = mDataBinding.etExpectedSalary.getText().toString().trim();
        if (TextUtils.isEmpty(expectedSalary)) {
            ToastUtils.showShort(getString(R.string.please_input_expected_salary));
            return;
        }
        String arrivalTime = mDataBinding.tvArrivalTime.getText().toString().trim();
        if (TextUtils.isEmpty(arrivalTime)) {
            ToastUtils.showShort(getString(R.string.please_sel_arrival_time));
            return;
        }
        jobData.setTargetJob(expectedPosition);
        jobData.setTargetCity(intendedCity);
        jobData.setTargetSalary(expectedSalary);
        jobData.setEntryTime(arrivalTime);
        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        module.setData(jobData);
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }

    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }


    /**
     * 初始化城市选择器
     */
    private void showCityView() {
        //条件选择器
        cityOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                try {
                    mDataBinding.tvIntendedCity.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                } catch (Exception e) {
                    mDataBinding.tvIntendedCity.setText(jsonBeans.get(options1).getREGION_NAME() +
                            jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                }
            }
        }).setTitleText(getString(R.string.intended_city))//标题
                .setSubCalSize(16)//确定和取消文字大小
                .setTitleSize(14)//标题文字大小
                .setTitleColor(Color.parseColor("#000000"))//标题文字颜色
                .setSubmitColor(Color.parseColor("#2262D0"))//确定按钮文字颜色
                .setCancelColor(Color.parseColor("#A4A4A4"))//取消按钮文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setContentTextSize(16)//滚轮文字大小
                .build();
        cityOptions.setPicker(jsonBeans, arrayLists);
    }

    private void showArrivalTimeDialog() {
        ArrivalTimeDialog arrivalTimeDialog = new ArrivalTimeDialog(mContext);
        arrivalTimeDialog.setListener(new ArrivalTimeDialog.Ilistener() {

            @Override
            public void clickConfirm(String arrivalTime) {
                mDataBinding.tvArrivalTime.setText(arrivalTime);
            }
        });
        arrivalTimeDialog.show();
    }

}